<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"llmysnow.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="现代浏览器网页渲染原理——大厂面试考点；深入了解关键渲染路径，减少回流与重绘；防止布局抖动(Layout Thrashing)；GPU加速避免重绘，加速页面呈现">
<meta property="og:type" content="article">
<meta property="og:title" content="优化2.渲染优化">
<meta property="og:url" content="http://llmysnow.top/posts/9847b5b5/index.html">
<meta property="og:site_name" content="奇幻的梦之旅">
<meta property="og:description" content="现代浏览器网页渲染原理——大厂面试考点；深入了解关键渲染路径，减少回流与重绘；防止布局抖动(Layout Thrashing)；GPU加速避免重绘，加速页面呈现">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/js-img/DOM%E6%A0%91.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/js-img/CSSOM%E6%A0%91.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/js-img/Render-Tree%E6%B8%B2%E6%9F%93%E6%A0%91.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203011555931.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203011610224.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203011746343.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203021123102.jpg">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203021521334.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203021758673.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203030921281.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203030935502.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203031001205.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203031011358.jpg">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203031022860.png">
<meta property="article:published_time" content="2022-04-23T11:27:04.404Z">
<meta property="article:modified_time" content="2022-04-23T11:27:47.127Z">
<meta property="article:author" content="LiLyn">
<meta property="article:tag" content="性能优化">
<meta property="article:tag" content="关键渲染路径">
<meta property="article:tag" content="回流重绘">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lilyn/pic/raw/master/js-img/DOM%E6%A0%91.png">

<link rel="canonical" href="http://llmysnow.top/posts/9847b5b5/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>优化2.渲染优化 | 奇幻的梦之旅</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="奇幻的梦之旅" type="application/atom+xml">
</head>
<!--即时预加载技术，在用户点击之前预先加载页面-->
<script src="https://cdn.jsdelivr.net/gh/TRHX/CDN-for-itrhx.com@2.0.2/js/instantclick-1.2.2.js" type="module"></script>
<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
	  <!--<a target="_blank" rel="noopener" href="https://github.com/llwodexue" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>-->
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">奇幻的梦之旅</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">http://llmysnow.top/</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://llmysnow.top/posts/9847b5b5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/logo.jpg">
      <meta itemprop="name" content="LiLyn">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="奇幻的梦之旅">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          优化2.渲染优化
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-04-23 19:27:04" itemprop="dateCreated datePublished" datetime="2022-04-23T19:27:04+08:00">2022-04-23</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" itemprop="url" rel="index"><span itemprop="name">性能优化</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>5k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>5 分钟</span>
            </span>
            <div class="post-description">现代浏览器网页渲染原理——大厂面试考点；深入了解关键渲染路径，减少回流与重绘；防止布局抖动(Layout Thrashing)；GPU加速避免重绘，加速页面呈现</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="关键渲染路径"><a href="#关键渲染路径" class="headerlink" title="关键渲染路径"></a>关键渲染路径</h2><blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/fi3ework/blog/issues/16">前端性能优化之关键路径渲染优化</a></p>
</blockquote>
<p>浏览器怎么把页面渲染出来，渲染过程分很多环节，就是 <strong>关键渲染路径（CRP）</strong></p>
<ul>
<li><code>CRP</code>：<code>Critical Rendering Path</code></li>
</ul>
<p><strong>浏览器渲染过程：</strong></p>
<p>JavaScript（触发视觉变化） -&gt; Style（浏览器对样式重新进行计算） -&gt; Layout（布局） -&gt; Paint（绘制） -&gt; Composite（合成）</p>
<ul>
<li>JavaScript：可以通过 JavaScript 实现一些页面上视觉上的变化，例如：添加 DOM 元素、jQuery实现动画、CSS 动画</li>
<li>Style：浏览器对样式重新计算，这个过程会根据选择器进行重新匹配，计算哪些元素 CSS 受到影响，新的规则是什么样的，应该绘制成什么样子</li>
<li>Layout：布局就是把元素按照样式绘制到页面上，实际上是几何问题，需要知道元素的大小、位置</li>
<li>Paint：真正把内容绘制到页面上：文字、图片、颜色、阴影等</li>
<li>Composite：合成，绘制会和这个合成联系，浏览器为了提高效率，并不是把所有东西都画在一个层里，会建多个图层，最后再把它们组合起来</li>
</ul>
<h3 id="DOM-树"><a href="#DOM-树" class="headerlink" title="DOM 树"></a>DOM 树</h3><ol>
<li>浏览器下载完 HTML 文档，就要把代码读进去，读进去的是文本，它先把这些文本转换成单个的字符</li>
<li>HTML 有很多标签，标签是通过一对尖括号标记出来的，这个尖括号就可以用作于识别，就可以把一些字符串理解成有含义的标记，这些标记最终被换成节点对象，放在链形数据结构里，如下图</li>
</ol>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/js-img/DOM%E6%A0%91.png"></p>
<h3 id="CSS-树"><a href="#CSS-树" class="headerlink" title="CSS 树"></a>CSS 树</h3><ul>
<li>当解释器遇到引用的 CSS，会先把资源下载下来，下载完成后对这个资源进行文本处理，把里面的标记全部识别出来，看样式是哪个节点的样式，然后用树形结构把这个关系存储起来，如下图</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/js-img/CSSOM%E6%A0%91.png"></p>
<h3 id="浏览器构建渲染树"><a href="#浏览器构建渲染树" class="headerlink" title="浏览器构建渲染树"></a>浏览器构建渲染树</h3><p>DOM 和 CSSOM 合并成 Render Tree，把内容和样式合在一起，让浏览器理解最终要把什么画在页面上。合并的时候会把需要显示的留下，不需要显示的删除，比如：span 节点的样式是 <code>display: none</code>，不需要显示在页面上，构造成渲染树后，span 节点就会被去掉</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/js-img/Render-Tree%E6%B8%B2%E6%9F%93%E6%A0%91.png"></p>
<h2 id="回流与重绘"><a href="#回流与重绘" class="headerlink" title="回流与重绘"></a>回流与重绘</h2><p><strong>布局（layouts）与绘制（paint）</strong></p>
<ul>
<li>渲染树只包含网页需要的节点</li>
<li>布局计算节点精确的 <strong>位置和大小</strong>——“盒模型”</li>
<li>绘制是像素画每个节点的过程</li>
</ul>
<h3 id="影响回流的操作"><a href="#影响回流的操作" class="headerlink" title="影响回流的操作"></a>影响回流的操作</h3><p>布局也叫回流。布局：通常页面第一次加载完成之后，把东西放在页面上。回流：由于页面上发生了视觉上的变化又再次导致重新布局</p>
<ul>
<li>添加/删除元素</li>
<li><code>display: none</code></li>
<li>移动元素位置</li>
<li>操作 <code>styles</code></li>
<li><code>offsetLeft</code>、<code>scrollTop</code>、<code>clientWidth</code></li>
<li>修改浏览器大小、字体大小</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203011555931.png"></p>
<h3 id="避免布局抖动"><a href="#避免布局抖动" class="headerlink" title="避免布局抖动"></a>避免布局抖动</h3><ul>
<li><p>避免回流</p>
<p>比如想改变元素位置，千万不要修改 top、left 这样的值，可以使用 <code>transform</code> 或 <code>translate</code> 来进行位移</p>
</li>
<li><p>读写分离</p>
<p>批量的读操作再进行批量写操作</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> cards = <span class="built_in">document</span>.getElementsByClassName(<span class="string">&#x27;card&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> update = <span class="function"><span class="params">timestamp</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; cards.length; i++) &#123;</span><br><span class="line">    cards[i].style.width = <span class="built_in">Math</span>.sin(cards[i].offsetTop + timestamp / <span class="number">1000</span> + <span class="number">1</span>) * <span class="number">500</span> + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">window</span>.requestAnimationFrame(update)</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">&#x27;load&#x27;</span>, update)</span><br></pre></td></tr></table></figure>

<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203011610224.png"></p>
<p>性能分析里右上角红色三角形表示长任务提示了强制回流（<code>Forced reflow</code>）</p>
<ul>
<li>浏览器为了提高布局性能，会尽量把修改布局相关属性推迟操作</li>
<li>但是当你获取布局属性（比如：<code>offsetTop</code>）时是无法推迟操作，不得不立即进行最新计算，以保证取到的是最新结果</li>
<li>如果有连续的读写，就会导致有连续不断的回流发生，导致页面抖动</li>
</ul>
<h3 id="使用-FastDom（防止布局抖动）"><a href="#使用-FastDom（防止布局抖动）" class="headerlink" title="使用 FastDom（防止布局抖动）"></a>使用 FastDom（防止布局抖动）</h3><blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/wilsonpage/fastdom">FastDom</a></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.bootcdn.net/ajax/libs/fastdom/1.0.10/fastdom.min.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">let</span> cards = <span class="built_in">document</span>.getElementsByClassName(<span class="string">&#x27;card&#x27;</span>)</span></span><br><span class="line"><span class="javascript">  <span class="keyword">const</span> update = <span class="function"><span class="params">timestamp</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; cards.length; i++) &#123;</span></span><br><span class="line"><span class="javascript">      fastdom.measure(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">let</span> top = cards[i].offsetTop</span></span><br><span class="line"><span class="javascript">        fastdom.mutate(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">          cards[i].style.width = <span class="built_in">Math</span>.sin(top + timestamp / <span class="number">1000</span> + <span class="number">1</span>) * <span class="number">500</span> + <span class="string">&#x27;px&#x27;</span></span></span><br><span class="line">        &#125;)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"><span class="javascript">    <span class="built_in">window</span>.requestAnimationFrame(update)</span></span><br><span class="line">  &#125;</span><br><span class="line"><span class="javascript">  <span class="built_in">window</span>.addEventListener(<span class="string">&#x27;load&#x27;</span>, update)</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>运行之后发现 load 之后没有再出现红色三角形的长任务警告了，也没有出现有问题的 layout</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203011746343.png"></p>
<h2 id="复合线程与图层"><a href="#复合线程与图层" class="headerlink" title="复合线程与图层"></a>复合线程与图层</h2><h3 id="复合线程"><a href="#复合线程" class="headerlink" title="复合线程"></a>复合线程</h3><p><strong>复合线程</strong>（<code>compositor thread</code>）</p>
<ul>
<li>将页面拆分图层进行绘制再进行复合</li>
<li>利用 DevTools 了解网页的图层拆分情况</li>
</ul>
<p><strong>页面是怎么拆成不同图层的：</strong></p>
<ul>
<li>默认情况下它是由浏览器决定的，浏览器会根据一些规则来判断是否将页面拆分成多个图层，又把哪些元素拆分成一个单独的图层（依据元素与元素之间是否有相互影响）</li>
<li>主动的把一些元素提取成一个单独的图层（知道元素会影响其他部分，可以提取出去）</li>
</ul>
<p><strong>Position、Scale、Rotation、Opacity</strong></p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://www.html5rocks.com/en/tutorials/speed/high-performance-animations/#:~:text=Modern%20browsers%20can%20animate%20four,%2C%20scale%2C%20rotation%20and%20opacity.">High Performance Animations</a></p>
</blockquote>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203021123102.jpg"></p>
<h3 id="减少重绘"><a href="#减少重绘" class="headerlink" title="减少重绘"></a>减少重绘</h3><ul>
<li>利用 DevTools 识别 paint 的瓶颈</li>
<li>利用 will-change 创建新的图层</li>
</ul>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">@keyframes</span> rotate &#123;</span><br><span class="line">  0% &#123;</span><br><span class="line">    <span class="attribute">transform</span>: <span class="built_in">rotate</span>(<span class="number">0deg</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  100% &#123;</span><br><span class="line">    <span class="attribute">transform</span>: <span class="built_in">rotate</span>(<span class="number">360deg</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.cardSpinning</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">300px</span>;</span><br><span class="line">  <span class="attribute">animation</span>: <span class="number">3s</span> linear infinite running rotate;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在 Performance 里录制动画并进行分析，放大后随便看一组，首先会重新计算样式（<code>Recalculate Style</code>）、更新图层数（<code>Update Layer Tree</code>），并没发生布局和重绘（<code>Paint</code>），直接触发复合（<code>Composite Layers</code>）</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203021521334.png"></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">@keyframes</span> rotate &#123;</span><br><span class="line">  0% &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">300px</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  100% &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">600px</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在 Network 里，键入 <code>Ctrl + Shift + P</code>，输入 <code>Show Rendering</code>，勾选 <code>Paint flashing</code>，所重绘的区域会用绿色标记出来</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203021758673.png"></p>
<p>可以利用 <code>willChange</code> 属性，这样浏览器就知道这个元素应当被提取到一个单独的图层里</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/will-change">will-change</a></li>
</ul>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">will-change</span>: <span class="selector-tag">transform</span>;</span><br></pre></td></tr></table></figure>

<h2 id="高频事件防抖"><a href="#高频事件防抖" class="headerlink" title="高频事件防抖"></a>高频事件防抖</h2><p>有一些事件触发频率非常高，甚至会超过帧的刷新速率，比如：<code>scroll</code>、<code>touchstart</code>、<code>touchmove</code>、<code>mousemove</code>。这些函数触发频率非常快，在 Main 函数里，可以看到事件处理函数消耗比较高，但实际上并没有必要在一帧里处理很多次，比如滚动，并不关心中心过程，只关心最后滚动到哪里，这样就可能会出现卡顿也就是抖动</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203030921281.png"></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 修改图片宽度</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">changeWidth</span>(<span class="params">rand</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> cards = <span class="built_in">document</span>.getElementsByClassName(<span class="string">&#x27;card&#x27;</span>)</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; cards.length; i++) &#123;</span><br><span class="line">    cards[i].style.width = (<span class="built_in">Math</span>.sin(rand / <span class="number">1000</span>) + <span class="number">1</span>) * <span class="number">500</span> + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 利用DevTools打开时可以复现抖动的问题</span></span><br><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">&#x27;pointermove&#x27;</span>, <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> pos = e.clientX</span><br><span class="line">  changeWidth(pos)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>先看一下下一帧的生命周期</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203030935502.png"></p>
<ul>
<li>事件（<code>touch</code>、<code>wheel</code>）开始触发，JS 触发视觉上的变化</li>
<li>一帧开始，rAF （<code>requestAnimationFrame</code>）调用，Layout （重新计算样式，更新图层）布局，Paint （合并图层）重绘</li>
<li>rAF 是在布局和重绘之前调用，这样可以利用 rAF 先把我们做的处理做完，之后再去进行布局和绘制，极大提高效率</li>
<li>rAF 本身是由 JavaScript 进行调度的，会尽量让你能够在每一次绘制之前去触发这个 rAF，尽量达到 60fps 的效果</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203031001205.png"></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 修改图片宽度</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">changeWidth</span>(<span class="params">rand</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> cards = <span class="built_in">document</span>.getElementsByClassName(<span class="string">&#x27;card&#x27;</span>)</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; cards.length; i++) &#123;</span><br><span class="line">    cards[i].style.width = (<span class="built_in">Math</span>.sin(rand / <span class="number">1000</span>) + <span class="number">1</span>) * <span class="number">500</span> + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> ticking = <span class="literal">false</span></span><br><span class="line"><span class="comment">// 利用DevTools打开时可以复现抖动的问题</span></span><br><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">&#x27;pointermove&#x27;</span>, <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> pos = e.clientX</span><br><span class="line">  <span class="keyword">if</span> (ticking) <span class="keyword">return</span></span><br><span class="line">  ticking = <span class="literal">true</span></span><br><span class="line">  <span class="built_in">window</span>.requestAnimationFrame(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    changeWidth(pos)</span><br><span class="line">    ticking = <span class="literal">false</span></span><br><span class="line">  &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="React-时间调度实现"><a href="#React-时间调度实现" class="headerlink" title="React 时间调度实现"></a>React 时间调度实现</h2><blockquote>
<p><a target="_blank" rel="noopener" href="https://juejin.cn/post/6844904196345430023">熟悉requestidlecallback到了解react ric polyfill实现</a></p>
</blockquote>
<p><strong>基本原理</strong></p>
<ul>
<li><p><code>requestIdleCallback</code> 的问题</p>
<p><code>requestIdleCallback</code> 官方给出的标准：它的执行希望在 16ms 时间内，如果还有空余时间，它可以让它做些事情，但是这个函数并没有被浏览器进行很好的支持，React 考虑到这点，是通过 rAF 模拟实现 rIC</p>
</li>
<li><p>通过 rAF 模拟 rIC</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203031011358.jpg"></p>
</li>
</ul>
<p><code>requestIdleCallback </code> 在一帧关键渲染周期内，都做的事情：</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203031022860.png"></p>
<ul>
<li><code>requestAnimationFrame</code> 是在 Layout 和 Paint 之前触发。在这一阵开始渲染之前执行</li>
<li><code>requestIdleCallback</code> 是在 Layout 和 Paint 之后触发。在一帧之内，如果所有事情都做完了还有剩余时间，就可以做一些其他的事情。但是这个事要有个度，因为要给主线程留更多的空余时间，因为一旦有交互过来，至少要留 50ms 给每一次交互去处理</li>
</ul>
<p><code>requestIdleCallback </code> React 里的实现：</p>
<ul>
<li>当用户不再看这个页面或页面不可见，<code>requestAnimationFrame</code> 是不会运行，React 需要借用这个函数即使在后台状态也要继续完成，所以需要找到替代方案能保证任务做完，用 setTimeout 来实现</li>
<li>作为调度函数，最关心的是所有任务，所以会给这些任务安排优先级，React 这边安排了 5个优先级，从立即可以执行到有空闲执行，另外这些任务都有过期时间，这些任务的存储肯定有一个队列，把这些队列排到队列里，然后等待 <code>requestIdleCallback</code> 有空闲时去执行，底层实现是双向环形链表</li>
</ul>

    </div>

    
    
    

	<div>
		  
			
<div class="my_post_copyright clearfix">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article class="box-left">
    <img src="https://gitee.com/lilyn/pic/raw/master/logo.jpg">
  </article>
  <article class="box-right">
    <p><span>本文标题:</span><a href="/posts/9847b5b5/">优化2.渲染优化</a></p>
    <p><span>文章作者:</span><a href="/" title="访问 LiLyn 的个人博客">LiLyn</a></p>
    <p><span>原始链接:</span><a href="/posts/9847b5b5/" title="优化2.渲染优化">http://llmysnow.top/posts/9847b5b5/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://llmysnow.top/posts/9847b5b5/"  aria-label="复制成功！"></i></span>
  </p>
    <p><span>许可协议:</span>转载请保留原文链接及作者。</p>  
  </article>
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>

		  
	</div>

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" rel="tag"><i class="fa fa-tag"></i> 性能优化</a>
              <a href="/tags/%E5%85%B3%E9%94%AE%E6%B8%B2%E6%9F%93%E8%B7%AF%E5%BE%84/" rel="tag"><i class="fa fa-tag"></i> 关键渲染路径</a>
              <a href="/tags/%E5%9B%9E%E6%B5%81%E9%87%8D%E7%BB%98/" rel="tag"><i class="fa fa-tag"></i> 回流重绘</a>
          </div>
		 
		<!--post-tags 彩色标签-->
		<script type="text/javascript">
		var tagsall=document.getElementsByClassName("post-tags")
		for (var i = tagsall.length - 1; i >= 0; i--){
			var tags=tagsall[i].getElementsByTagName("a");
			for (var j = tags.length - 1; j >= 0; j--) {
				var r=Math.floor(Math.random()*75+130);
				var g=Math.floor(Math.random()*75+100);
				var b=Math.floor(Math.random()*75+80);
				tags[j].style.background = "rgb("+r+","+g+","+b+")";
				tags[j].style.color = "#fff";
				tags[j].style.padding = "0 10px";
			}
		}                        
		</script>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/8f9af1b2/" rel="prev" title="优化1.性能优化的指标和工具">
      <i class="fa fa-chevron-left"></i> 优化1.性能优化的指标和工具
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/32a9efe/" rel="next" title="优化3.代码优化">
      优化3.代码优化 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B3%E9%94%AE%E6%B8%B2%E6%9F%93%E8%B7%AF%E5%BE%84"><span class="nav-text">关键渲染路径</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#DOM-%E6%A0%91"><span class="nav-text">DOM 树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CSS-%E6%A0%91"><span class="nav-text">CSS 树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E6%9E%84%E5%BB%BA%E6%B8%B2%E6%9F%93%E6%A0%91"><span class="nav-text">浏览器构建渲染树</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9E%E6%B5%81%E4%B8%8E%E9%87%8D%E7%BB%98"><span class="nav-text">回流与重绘</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BD%B1%E5%93%8D%E5%9B%9E%E6%B5%81%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="nav-text">影响回流的操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%81%BF%E5%85%8D%E5%B8%83%E5%B1%80%E6%8A%96%E5%8A%A8"><span class="nav-text">避免布局抖动</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-FastDom%EF%BC%88%E9%98%B2%E6%AD%A2%E5%B8%83%E5%B1%80%E6%8A%96%E5%8A%A8%EF%BC%89"><span class="nav-text">使用 FastDom（防止布局抖动）</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%8D%E5%90%88%E7%BA%BF%E7%A8%8B%E4%B8%8E%E5%9B%BE%E5%B1%82"><span class="nav-text">复合线程与图层</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%8D%E5%90%88%E7%BA%BF%E7%A8%8B"><span class="nav-text">复合线程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%8F%E5%B0%91%E9%87%8D%E7%BB%98"><span class="nav-text">减少重绘</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%AB%98%E9%A2%91%E4%BA%8B%E4%BB%B6%E9%98%B2%E6%8A%96"><span class="nav-text">高频事件防抖</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#React-%E6%97%B6%E9%97%B4%E8%B0%83%E5%BA%A6%E5%AE%9E%E7%8E%B0"><span class="nav-text">React 时间调度实现</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LiLyn"
      src="/images/logo.jpg">
  <p class="site-author-name" itemprop="name">LiLyn</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">70</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">162</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/llwodexue" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;llwodexue" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/qq_38689395" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_38689395" rel="noopener" target="_blank"><i class="fas fa-copyright fa-fw"></i>CSDN</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://scx69.gitee.io/blogs/" title="https:&#x2F;&#x2F;scx69.gitee.io&#x2F;blogs&#x2F;" rel="noopener" target="_blank">花开满城</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yixin-oss.gitee.io/daytoy/" title="http:&#x2F;&#x2F;yixin-oss.gitee.io&#x2F;daytoy&#x2F;" rel="noopener" target="_blank">daytoy</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LiLyn</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">463k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:01</span>
</div>

<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>-->

<!--
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>-->

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  <!-- 动态线条背景 -->
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  <!-- 页面点击小红心 -->
  <script type="text/javascript">
	!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
  </script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body>
</html>